package com.example.demoauthclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Controller
public class ClientController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/result")
    public String hello(String code, Model model) {
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
            map.add("client_id", "client_1");
            map.add("client_secret", "mysecret");
            map.add("code", code);
            map.add("redirect_uri", "http://localhost:8780/result");
            map.add("scope", "webclient");
            map.add("grant_type", "authorization_code");

            /* public String hello(String username, String password, Model model
               map.add("username", username);
               map.add("password", password);
               map.add("client_id", "client_1");
               map.add("client_secret", "mysecret");
               map.add("grant_type", "password");)*/

            /* public String hello(Model model)
               map.add("client_id", "client_1");
               map.add("client_secret", "mysecret");
               map.add("grant_type", "client_credentials"); */

            //grant_type：implicit不需要此处的controller，前端直接获取token，并用js解析即可
            //此时auth-server中redirect-uri改为：http://localhost:8780/index.html，另外auth-service中要做跨域配置

            System.out.println(map);
            Map<String,String> resp = restTemplate.postForObject("http://localhost:8770/oauth/token", map, Map.class);
            String access_token = resp.get("access_token");
            System.out.println("Token-----"+access_token);
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Bearer " + access_token);
            HttpEntity<Object> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<String> entity = restTemplate.exchange("http://localhost:8790/order/100", HttpMethod.GET, httpEntity, String.class);
            model.addAttribute("msg1", entity.getBody());
            model.addAttribute("code", code);
            model.addAttribute("token", access_token);
            entity = restTemplate.exchange("http://localhost:8790/order/repo/500", HttpMethod.GET, httpEntity, String.class);
            model.addAttribute("msg2", entity.getBody());

//            entity = restTemplate.exchange("http://localhost:8791/repository/500", HttpMethod.GET, httpEntity, String.class);
//            model.addAttribute("msg2", entity.getBody());
        return "index";
    }

    @GetMapping("/hello")
    @ResponseBody
    public  String hello() {
        return "Hello Client1";
    }

}
